﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VDC.COMMON;
using System.Reflection;
using System.Net.Mail;
using System.Net;

namespace VDC.VONE.WSERIVCE {

    public class SendEmail {
        /// <summary>
        ///    MailHost session key.
        /// </summary>
        private const string MailHost = "MailHost";

        /// <summary>
        ///    MailPort session key.
        /// </summary>
        private const string MailPort = "MailPort";

        /// <summary>
        ///    EnableSSLMail session key.
        /// </summary>
        private const string EnableSSLMail = "EnableSSLMail";

        /// <summary>
        ///    NeedNetworkCredential session key.
        /// </summary>
        private const string NeedNetworkCredential = "NeedNetworkCredential";

        /// <summary>
        ///    SystemMail session key.
        /// </summary>
        private const string SystemMail = "SystemMail";

        /// <summary>
        ///    MailPassword session key.
        /// </summary>
        private const string MailPassword = "MailPassword";

        /// <summary>
        /// The name method.
        /// </summary>
        private const string NameMethod = ":::::::::::::::::::SendMail:::::::::::::::::";

        public static string SendMail(string toList, string from, string ccList, string subject, string body) {
        string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
        LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
        string msg = string.Empty;
        try {
        MailMessage message = new MailMessage();
        //SmtpClient smtpClient = new SmtpClient();

        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (!string.IsNullOrEmpty(ccList)) {
        message.CC.Add(ccList);
        }

        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;

        var smtpClient = new SmtpClient(PublicFuncs.GetAppSettings(MailHost), Convert.ToInt32(PublicFuncs.GetAppSettings(MailPort))) {
            Credentials = new NetworkCredential(PublicFuncs.GetAppSettings(SystemMail), PublicFuncs.GetAppSettings(MailPassword)),
            EnableSsl = true
        };


        if (smtpClient != null) {
        smtpClient.Send(message);

        msg = Constants.Success + " mail to : " + toList;
        LogHelper.Instance.WriteInfo(NameMethod + msg);
        }
        } catch (Exception ex) {
        msg = ex.Message;
        LogHelper.Instance.WriteInfo(NameMethod + msg);
        }

        LogHelper.Instance.WriteInfo(Constants.End + methodName);
        return msg;
        }
    }
}